define(function(require, exports, module){

	var containerId='container';

	container.prototype = {

		init:function(mId,tpl){
			this.pageId = mId;
			this.dom = $('#'+this.pageId).find('#'+containerId);
			this.renderSearchHistory();
			this.bindEvt();
		},

		reload :function(mId){
			var dom=$('#'+mId).find('#'+containerId),play = window.controller.play.play;
			dom.find('.container_search_history').hide();
			dom.find('.body_box').show();
			if(play) dom.find('.ico_music_animate').addClass('selected')
			else dom.find('.ico_music_animate').removeClass('selected')
		},

		renderSearchHistory:function(){
			var dom = this.dom;
			var history = util.getLocalStorage('search_history'),html='',
				domP = dom.find('.container_search_history p');
			_.each(history,function(i){
				html += "<li><div><i class='ico_history'></i>"+i+"<span class='ico_delete'></span></div></li>"
			});

			dom.find('.container_search_history ul').html(html);

			if(!history) domP.text('没有历史记录');
			else domP.text('清空历史记录');
		},

		searchSwitch:function(bool){
			var dom = this.dom;
			if(bool=='show'){
				dom.find('.body_box').hide();
				dom.find('.container_search_history').show();
			}else{
				dom.find('.body_box').show()
				dom.find('.container_search_history').hide()
			}
		},

		bindEvt :function(){
			var tthis = this,dom = this.dom,
				domHistory = dom.find('.container_search_history'),
				domHead = dom.find('.container_head'),
				domFooter = dom.find('.container_footer');

			domHead.find('.ico_music_animate').on('tap',function(){
				var songid = $('#play').attr('songid');
				var type = $('#play').attr('type');
				songid || (songid = 246946436);
				window.location.href = '#play?songid='+songid;
			});

			domHead.find('.ico_home').on('tap',function(){
				window.location.href ='#found';
				share.blur(tthis.pageId);
				share.tabSwitch($('#'+tthis.pageId),0);
				setTimeout(function(){
					$('#'+tthis.pageId).find('.body').scrollTop(1);
				},20)
				tthis.searchSwitch('hide');
			})

			domHead.find('.input_box .ico_delete').on('tap',function(){
				$(this).parent().find('.search').val('');
				$(this).hide();
			});

			domHead.find('.search').on('focus',function(){
				dom.find('body_box').show();
			});

			domHead.find('.search').on('blur',function(){
				domHead.find('.ico_music_animate').show();
				domHead.find('.search_close').hide();
				// tthis.searchSwitch('hide');
			});

			domHead.find('.search').on('focus',function(){
				tthis.searchSwitch('show');
				if($(this).val()){
					$(this).parent().find('.ico_delete').show();
				}
				domHead.find('.search_close').show();
				domHead.find('.ico_music_animate').hide();
			});

			domHead.find('.search_close').on('tap',function(){
				share.blur(tthis.pageId);
				tthis.searchSwitch('hide');
			});

			domHead.find('.search').on('keypress',function(e){
				$(this).parent().find('.ico_delete').show();
			});

			domHead.find('.search').on('keydown',function(e){
				if(!$(this).val()) return;
				
				if(e.keyCode==13){
					var val = $(this).val(),newArr=[];
					var history = util.getLocalStorage('search_history');
					if(history){
						var arrIndex = _.indexOf(history,val);
						if(arrIndex != -1){
							var curIndex = $.inArray(val,history);
							history.unshift(history[arrIndex]);
							history.splice(arrIndex+1,1);
						}
						else{
							history.unshift(val);
						}
						newArr = history;
					}else{
						newArr.unshift(val);
					}

					util.setLocalStorage('search_history',newArr)
					window.location.href = '#search?q=' + encodeURI(val);
				}
				if(e.keyCode == 8){
					if($(this).val().length <= 1){
						domHead.find('.ico_delete').attr('cc',1);
						domHead.find('.ico_delete').hide();
					}
				}
			});

			domHistory.on('tap',function(e){
				var target=$(e.target),
					curLi = target.closest('li'),
					curP = target.closest('p'),
					curSpan = target.closest('span');
				if(curSpan.hasClass('ico_delete')){
					curSpan.parents('li').remove();
					var getLoca = util.getLocalStorage('search_history');
					getLoca.splice($.inArray(curSpan.parents('li').text(),getLoca),1)
					util.setLocalStorage('search_history',getLoca);
					return false;
				}
				if(curLi.text()){
					var text = curLi.text();
					window.location.href='#search?q='+text;
				}
				if(curP.html()){
					localStorage.removeItem('music_search_history');
					domHistory.html('<p>没有历史记录</p>');
				}
			});

			domFooter.find('.playing').on('tap',function(){
				var songid = $('#play').attr('songid');
				songid || (songid = 246946436);
				window.location.href = '#play?songid='+songid;
			})

			domFooter.find('li').on('tap',function(){
				$(this).parent().find('li').removeClass('selected');
				$(this).addClass('selected');
			})

			domFooter.find('.tj').on('tap',function(){
				window.location.href = '#found';
				share.tabSwitch(dom,0);
				dom.find('.body').scrollTop(1);
			});
		},
	};

	function container(){
		return this;
	}

	function newContainer(){
		var obj = new container();
		this.init = function(mId,tpl){
			var newfun = new container();
			newfun.init(mId,tpl);
		}
		obj.init = this.init;
		return obj;
	};
	
	module.exports = new newContainer;
});